package team.hyznrj.studentsys.controller;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.annotations.Api;
import team.hyznrj.studentsys.dto.ListParamEntity;
import team.hyznrj.studentsys.entity.DataReport;
import team.hyznrj.studentsys.entity.ExcelAttribute;
import team.hyznrj.studentsys.entity.WriteExcelConditionEntity;
import team.hyznrj.studentsys.service.ExcelService;
import team.hyznrj.studentsys.service.FileService;
import team.hyznrj.studentsys.service.ParamService;
import team.hyznrj.studentsys.utils.ValueUtil;
@RestController
@RequestMapping("export")
@Api(value = "export")
public class ExportController {
	@Value("${StudentSavePath}")
	private String savePath;
	@Autowired
	FileService fileService;
	@Autowired
	ExcelService excelService;
	@Autowired
	ParamService paramService;
	/**
	 * 设置导入参数
	 * @param conditions
	 * @param request
	 * @return
	 */
	private ExcelAttribute exportSetting(WriteExcelConditionEntity conditions,HttpServletRequest request){
		String flag=(String) request.getAttribute("flag");
		conditions.setFlag(flag);
		ExcelAttribute excel=new ExcelAttribute();
		List<Integer> sheetPage=new ArrayList<>();
		sheetPage.add(0);
		excel.setSheetPage(sheetPage);
		String path=savePath+new ObjectId().toString()+".xls";
		excel.setDirectPath(path);
		return excel;
	}
	/**
	 * 对url进行加密
	 * //TODO
	 * @param data
	 * @throws UnsupportedEncodingException 
	 */
	private void dataSetting(DataReport data,String flag) throws UnsupportedEncodingException{
		data.setData(ValueUtil.codeUrl((String) data.getData(),flag));
	}
	/**
	 * 导出学生信息表(可选列)
	 * @param conditions 查询条件
	 * @param column 导出列id或值
	 * @param columnIndex 该列在excel中的位置
	 * @return
	 * @throws IOException
	 */

	@RequestMapping(value = "/studentMessage")
	public DataReport exportStudentMessage(WriteExcelConditionEntity conditions,ListParamEntity list,HttpServletRequest request) throws IOException{
		ExcelAttribute excel = exportSetting(conditions,request);
		DataReport writeExcelWithSGQ = (DataReport)excelService.WriteExcelWithStudentMessage(conditions, excel, list.getColumn(), list.getColumnIndex());
		dataSetting(writeExcelWithSGQ,(String) request.getAttribute("flag"));
		return writeExcelWithSGQ;
	}
	/**
	 * 导出成绩表
	 * @param conditions 查询条件
	 * @return
	 * @throws IOException
	 */
	@RequestMapping(value = "/courseAchievement")
	public DataReport exportCourseAchievement(WriteExcelConditionEntity conditions,HttpServletRequest request) throws IOException{
		ExcelAttribute excel = exportSetting(conditions,request);
		DataReport writeExcelWithSGQ = (DataReport)excelService.WriteExcelWithCourseAchievement(conditions, excel);
		dataSetting(writeExcelWithSGQ,(String) request.getAttribute("flag"));
		return writeExcelWithSGQ;
	}
	/**
	 * 导出操行分表
	 * @param conditions 查询条件
	 * @return
	 * @throws IOException
	 */
	@RequestMapping(value = "/qualityAchievement")
	public DataReport exportQualityAchievement(WriteExcelConditionEntity conditions,HttpServletRequest request) throws IOException{
		ExcelAttribute excel = exportSetting(conditions,request);
		DataReport writeExcelWithSGQ = (DataReport)excelService.WriteExcelWithQualityMarks(conditions, excel);
		dataSetting(writeExcelWithSGQ,(String) request.getAttribute("flag"));
		return writeExcelWithSGQ;
	}
	/**
	 * 导出综合测评
	 * @param conditions 查询条件
	 * @return
	 * @throws IOException
	 */
	
	
	//TODO 综合测评 2018年9月13日 出现新表格，所以由用户自行判断导出何种表
	@RequestMapping(value = "/comprehensiveAssessment/a")
	public DataReport exportComprehensiveAssessment_a(WriteExcelConditionEntity conditions,HttpServletRequest request) throws IOException{
		return exportComprehensiveAssessmentExcel(conditions,request,false);
	}
	@RequestMapping(value = "/comprehensiveAssessment/b")
	public DataReport exportComprehensiveAssessment_b(WriteExcelConditionEntity conditions,HttpServletRequest request) throws IOException{
		return exportComprehensiveAssessmentExcel(conditions,request,true);
	}
	private DataReport exportComprehensiveAssessmentExcel(WriteExcelConditionEntity conditions,HttpServletRequest request,boolean isNew) throws IOException{
		ExcelAttribute excel = exportSetting(conditions,request);
		DataReport writeExcelWithSGQ = (DataReport)excelService.WriteExcelWithSGQ(conditions,
				excel,isNew);
		dataSetting(writeExcelWithSGQ,(String) request.getAttribute("flag"));
		return writeExcelWithSGQ;
	}
}
